<?php

use think\Db;
use think\Session;

/**
 * 获取指定栏目下面的日，周，月排行
 * @param type $column_id 栏目id 如为空默认获取排行版
 * @return type
 */
function getColumnRanking ($column_id=0,$limit=10) {
    
    $rankList = array();
    $last_time = Db::name('Movies')->cache(3600)->limit(1)->order('id desc')->find()['add_time'];
    $end_time = $last_time;
    
    //本日  --应要求获取最后一篇文章往前推1个工作日
    $start_time = $last_time-86400;
    
    $rankList['ri'] = getChildMovies($column_id,'click desc',$limit,['add_time'=>['between',"$start_time,$end_time"]])['data'];
    
    //本周  --应要求获取最后一篇文章往前推1周
    $start_time = $last_time-(86400*7);
    $rankList['zhou'] = getChildMovies($column_id,'click desc',$limit,['add_time'=>['between',"$start_time,$end_time"]])['data'];
    
    
    //本月 --应要求获取最后一篇文章往前推1月
    $start_time = $last_time-(86400*30);
    $rankList['yue'] = getChildMovies($column_id,'click desc',$limit,['add_time'=>['between',"$start_time,$end_time"]])['data'];
    
    return $rankList;
}

/**
 * 获取文章的日，周，月排行
 * @param type $column_id 栏目id 如为空默认获取排行版
 * @return type
 */
function getColumnRanking2 ($limit=10) {
    
    $rankList = array();
    $last_time = Db::name('Article')->cache(3600)->limit(1)->order('id desc')->find()['publish_time'];
    $end_time = $last_time;
    
    //本日  --应要求获取最后一篇文章往前推1个工作日
    $start_time = $last_time-86400;
    
    $rankList['ri'] = Db::name('Article')->cache(3600)->field('id,title')->where(['publish_time'=>['between',"$start_time,$end_time"]])->order('reading desc')->limit($limit)->select();
    
    //本周  --应要求获取最后一篇文章往前推1周
    $start_time = $last_time-(86400*7);
    $rankList['zhou'] = Db::name('Article')->cache(3600)->field('id,title')->where(['publish_time'=>['between',"$start_time,$end_time"]])->order('reading desc')->limit($limit)->select();
    
    
    //本月 --应要求获取最后一篇文章往前推1月
    $start_time = $last_time-(86400*30);
    $rankList['yue'] = Db::name('Article')->cache(3600)->field('id,title')->where(['publish_time'=>['between',"$start_time,$end_time"]])->order('reading desc')->limit($limit)->select();
    
    return $rankList;
}





/**
 * 获取推荐的标签
 * @param limit 获取多少个
 * @return type
 */
function getLabel ($limit=9) {
    $label_list = Db::name('Label')->field('id,label_name,pingying')->where(['is_recommend'=>1])->order('sort asc')->limit($limit)->select();

   return $label_list; 
}

/**
 * 获取最新专题
 * @param limit 获取多少个
 * @return type
 */
function newSubject ($limit=4) {
    $subject_list = Db::name('Subject')->field('id,name,thumb,pingying')->order('id desc')->limit($limit)->select();
    return $subject_list; 
}

/**
 * 根据栏目id获取子栏目的文章
 * @param col_id 栏目id
 * @param limit 获取多少个
 * @return type
 */
function getColArticle ($col_id,$limit=4) {
    $article_list = Db::name('Article')->where(['cid'=>$col_id,'status'=>1])->field('id,title,thumb,introduction,publish_time')->order('id desc')->limit($limit)->select();
    return $article_list;
}

/**
 * 根据栏目id获取医院子栏目的文章
 * @param col_id 栏目id
 * @param limit 获取多少个
 * @return type
 */
function getYColArticle ($col_id,$limit=10) {
    $article_list = Db::name('Article2')->where(['cid'=>$col_id,'is_hot'=>1])->field('id,title,thumb,introduction,publish_time')->order('id desc')->limit($limit)->select();
    return $article_list;
}


/**
 * 根据条件获取文章
 * @param col_id 栏目id
 * @param limit 获取多少个
 * @return type
 */
function getWhereArticle ($where=[],$limit=10) {
    $article_list = Db::name('Article')->where($where)->field('id,cid,title,thumb,introduction,publish_time')->order('reading desc')->limit($limit)->select();
    return $article_list;
}

//根据pid获取所有的子元素
function getCategoryChildren($pid,$is_screening=1)
{
    $children = Db::name('category')->cache(3600)->where(['pid' => $pid,'is_screening'=>$is_screening])->order('sort asc')->select();
    return $children;
}


/**
* 登录时创建会话SESSION
* @param type $user_info
* @return type
*/
function createSession($user_info = array()) {
   if (empty($user_info) || !is_array($user_info))
        return;
   
        Session::set('user_info', $user_info);

        //登录时间 ip 次数更新 
        $update_info = array(
            'login_num' => ($user_info['login_num'] + 1),
            'last_login_time' => time(),
            'last_login_ip' => request()->ip(),
        );
        Db::name('User')->where(['id'=>$user_info['id']])->update($update_info);
}


    /**
     * 获取该栏目下的电影排行（所有子栏目的）
     * @param type $id
     * @param type $order
     * @return type
     */
    function getChildMovies ($id,$order,$page=10,$where=[]) {
        $ids = '';
        foreach (cache('Category_list') as $k=>$v) {
            if($v['pid'] == $id){
               $ids.=$v['id'].','; 
            }
        }
        
        $ids = $ids ? ['in',trim($ids,',')] : $id;
        
        $data = Db::name('Movies')->cache(3600)->field('id,title,cover_image,introduction,click,add_time')->where(['pid'=>$ids])->where($where)->order($order)->paginate($page);
        
        $page = $data->render();
        $data = $data->toArray();
        
//        foreach ($data['data'] as $k=>$v){  //不能访问的图片直接显示默认图
//            $data['data'][$k]['cover_image'] = url_exists($v['cover_image']) ? $v['cover_image'] : '/static/home/images/default.png';
//        }
        
        return [
            'page'=>$page,
            'data'=>$data['data']
        ];
    }